﻿using App.Common;
using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;
using System.IO;
using System;

public class MapEditorUI : UIBase
{
    public int MapID;
    GameObject Map;
    public override void OnDisplay(object args)
    {
        GameMgr.Instance.InitModules();

        StartCoroutine(saveRoomMap(MapID));
    }

    IEnumerator saveRoomMap(int MapID)
    {
        if (MapID == -1)
        {
            var list = ResourceManager.Instance.LoadAll<GameObject>("Prefabs/Map/");
            for (int i = 2; i <= list.Length; i++)
            {
                Map = ResourceManager.Instance.Instantiate("Prefabs/Map/MapEditor" + i.ToString());
                yield return 2;
                SaveRoomMap(i);
            }
        }
        else
        {
            Map = ResourceManager.Instance.Instantiate("Prefabs/Map/MapEditor" + MapID.ToString());
            yield return 2;
            SaveRoomMap(MapID);
        }
    }
    public void SaveRoomMap(int MapID)
    {
        var savePath = Application.dataPath + "/Resources/Text/MapData/Map" + MapID.ToString() + ".json";
        if (!File.Exists(savePath))
        {
            File.Create(savePath);
        }
        var data = MapEditorBase.GetRoomData();
        data.mapID = MapID;
        var save = JsonConvert.SerializeObject(data);
        try
        {
            File.WriteAllText(savePath, save);
        }
        catch
        {

        }
        Debug.Log(save);
        Destroy(Map);
    }
}

